<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-select v-model="queryParams.area" size="small" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            type="warning"-->
      <!--            plain-->
      <!--            icon="el-icon-download"-->
      <!--            size="mini"-->
      <!--            @click="handleExport"-->

      <!--        >导出</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="rowList">
      <el-table-column label="记录单号" align="center" prop="id" width="120" fixed="left"/>
      <el-table-column label="审核状态" prop="approveStatus" width="160" align="center">
        <template slot-scope="scope">
          <span @click="handleApprove(scope.row)" :style="{'color': colors(scope.row.approveStatus), 'cursor': 'pointer'}">{{ scope.row.approveStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="能耗数值" align="center" prop="amount" width="120" />
      <el-table-column label="园区" align="center" prop="area" width="120" />
      <el-table-column label="具体位置" align="center" prop="location" width="180" />
      <el-table-column label="记录人" prop="createBy" width="120" align="center"/>
      <el-table-column label="记录日期" align="center" prop="recordTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="能耗数值" prop="amount">
          <el-input-number v-model="form.amount"  :precision="2" :controls="false" placeholder="请输入能耗数值" clearable :style="{width: '100%'}">
          </el-input-number>
        </el-form-item>
        <el-form-item label="记录时间" prop="recordTime">
          <el-date-picker v-model="form.recordTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                          :style="{width: '100%'}" placeholder="请选择记录时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="园区" prop="area">
          <el-select v-model="form.area"  clearable placeholder="请选择园区">
            <el-option label="二区" value="二区"></el-option>
            <el-option label="一区" value="一区"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="具体位置" prop="location">
          <el-select v-model="form.location" placeholder="请选择具体位置">
            <el-option v-for="item in monitorLocationList" :label="item.dictLabel" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, get, add, update, del } from "@/api/monitor/energy";
import { getDicts } from "@/api/system/dict/data";
import {blurRule, changeRule} from "@/utils/formRules";
export default {
  name: "Water",
  dicts: ['user_area'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      rowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        area:'',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        amount:blurRule('请输入能耗数值'),
        recordTime:changeRule('请选择记录时间'),
        area:changeRule('请选择园区'),
        location:changeRule('请选择具体位置'),
      },
      monitorLocationList:[]
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
    this.getDictData()
  },
  methods: {
    colors(status) {
      if (status === "通过") {
        return '#2a9c4a'
      } else if (status === "拒绝") {
        return '#FF0000'
      } else if (status === "审批中") {
        return '#fe9f29'
      } else {
        return '#fe9f29'
      }
    },
    handleApprove(row) {
      if (row.approveStatus === "未提交审批") {
        this.$modal.msgWarning("未提交审批");
      } else {
        this.$router.push("/approve/detail/" + row.approveMissionId);
      }
    },
    handleExport() {
      this.download('monitor/energy/export', {
        ...this.queryParams
      }, `energy_record_${new Date().getTime()}.xlsx`)
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.type = "水";
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.rowList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {};
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.type = "水";
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除编号为"' + row.id + '"的数据项？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //获取字典数据
    getDictData(){
      getDicts('monitor_location').then(res=>{
        this.monitorLocationList=res.data
      })
    },
  }
};
</script>
